Skip to content

BSL-39-AdminEventsAPI#37

Merged
rohanarcot merged 5 commits intomainfrom
bsl-39-admin-events-api
Apr 17, 2026
Merged

BSL-39-AdminEventsAPI#37
rohanarcot merged 5 commits intomainfrom
bsl-39-admin-events-api

Conversation

@sanjanamanivannan
Copy link
Copy Markdown
Contributor

No description provided.

Comment thread app/api/admin/events/route.ts Outdated
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { auth } from "@/auth";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { hasRole } from "@/lib/rbac";

this logic already exists in lib/rbac.ts, you can use the hasRole function

Comment thread app/api/admin/events/route.ts Outdated
Comment on lines +5 to +7
function isAmbassadorOrHigher(role: string) {
return role === "AMBASSADOR" || role === "SUPER_ADMIN";
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove this and replace with hasRole (will make the comment below again)

Comment thread app/api/admin/events/route.ts Outdated
return { ok: false as const, status: 401 as const, error: "Unauthorized" };
}

if (!isAmbassadorOrHigher(user.role)) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace this with if (!hasRole(user.role, "AMBASSADOR")) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So delete the isAmbassadorOrHigher function entirely and import hasRole (I made these comments above)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also move this hasRole conditional check above the findUnique function above

async function requireAmbassadorOrHigher() {
const session = await auth();
const email = session?.user?.email;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add const role = session?.user?.role;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rols is already in the session token set by auth.ts so you do not need to query the DB for it on every request. Can use session?.user?.role directly and only query the DB for the user id (which is needed for createdByUserID in POST)

Comment thread app/api/admin/events/route.ts Outdated
const session = await auth();
const email = session?.user?.email;

if (!email) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this if (!email || !role) {

Comment thread app/api/admin/events/route.ts Outdated

const user = await prisma.user.findUnique({
where: { email },
select: { id: true, role: true },
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove role: true

Comment thread app/api/admin/events/route.ts Outdated
Comment on lines 40 to 45
const events = await prisma.event.findMany({
orderBy: { startAt: "asc" },
});

return NextResponse.json(events);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add try/catch here.. it would look something like this:

  try {
    const events = await prisma.event.findMany({
      orderBy: { startAt: "asc" },
    });
    return NextResponse.json(events);
  } catch (err) {
    console.error("GET /api/admin/events:", err);
    return NextResponse.json({ error: "Failed to fetch events" }, { status: 500 });
  }

(Also have to do it for POST below.. ill leave comment)

Comment thread app/api/admin/events/route.ts Outdated
Comment on lines 80 to 92
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add try/catch like above.. something like this:

 try {
    const created = await prisma.event.create({
      data: {
        title: body.title,
        description: body.description ?? null,
        startAt,
        endAt,
        location: body.location ?? null,
        link: body.link ?? null,
        createdByUserId: gate.user.id,
      },
    });
    return NextResponse.json(created, { status: 201 });
  } catch (err) {
    console.error("POST /api/admin/events:", err);
    return NextResponse.json({ error: "Failed to create event" }, { status: 500 });
  }
}

@rohanarcot rohanarcot merged commit a6887d2 into main Apr 17, 2026
@rohanarcot rohanarcot deleted the bsl-39-admin-events-api branch April 17, 2026 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants